#pragma once
#include "stdafx.h"
#include "CheckFitness.h"
#include "GbcParser.h"
#include "CornerDetect.h"
#include <string>
#include "ScriptParser.h"
#include <set>
#include <list>
#include <utility>

using namespace std;

int SNU_Check_HoleSize(char** ppImageArrays, CCornerInfo* pCornerInfo, std::pair<int,int> point)
{
	int nHoleSize = 0;
	int nGridSize = 10;
	for ( int dx=-nGridSize; dx<=nGridSize; dx++ )
		for ( int dy=-nGridSize; dy<=nGridSize; dy++ )
		{
			int nNewX = point.first + dx;
			int nNewY = point.second + dy;
			if ( (pCornerInfo->GetWidth() > nNewX) && (nNewX>=0)
				&& (pCornerInfo->GetHeight() > nNewY) && (nNewY>=0)
				&& (unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(pair<int,int>(nNewX, nNewY), 3)] == 0xff )
			{
				nHoleSize ++;
			}
		}
	return nHoleSize;
	/*list< pair<int,int> > listQueue;
	set<pair<int,int>> setDupcheck;

	listQueue.push_back(point);
	setDupcheck.insert(point);
	while ( !listQueue.empty() )
	{
		pair<int,int> pairCur = *listQueue.begin();
		listQueue.pop_front();

		for ( int dx=-1; dx<=1; dx++ )
			for ( int dy=-1; dy<=1; dy++ )
			{
				int nNewX = pairCur.first + dx;
				int nNewY = pairCur.second + dy;
				if ( (dx!=0 || dy!=0)
					&& (pCornerInfo->GetWidth() > nNewX) && (nNewX>=0)
					&& (pCornerInfo->GetHeight() > nNewY) && (nNewY>=0)
					&& (unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(pair<int,int>(nNewX, nNewY), 3)] == 0xff )
				{
					pair<int, int> pairNew(nNewX, nNewY);
					pair< set< pair<int,int> >::iterator, bool> pairResult = setDupcheck.insert(pairNew);
					if ( pairResult.second )
						listQueue.push_back(pairNew);
				}
			}
	}
	return setDupcheck.size();*/
}

bool SNU_CHECK_FITNESS_HOLE(char** ppImageArrays, CCornerInfo* pCornerInfo, int nBillType, int nBillDirection)
{
	int scale = 0xff;

	int Width = pCornerInfo->GetWidth();
	int Height = pCornerInfo->GetHeight();

	int holesize = 0;

	std::pair<int,int> point;

	for (int j = 10; j < Height - 10; j += 5)
	{
		point.second = j;
		for (int i = 10; i < Width - 10; i += 5)
		{
			point.first = i;
			if ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point,3)] == scale)
			{
				holesize = SNU_Check_HoleSize(ppImageArrays, pCornerInfo, point);
				if (holesize > 39)
					return false;
			}
		}
	}

	return true;
}